import 'dart:io';
import 'package:number_spark_flutter/utils/ns_dialog_util.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

class NSPermissionUtil {
  static Future reqStoragePermission({Function? onSuccessAction}) async {
    PermissionStatus _permissionStatus;// = await Permission.photos.status;
    if (Platform.isAndroid){
      _permissionStatus = await Permission.storage.status;
    }else{
      _permissionStatus = await Permission.photos.status;
    }
    if (_permissionStatus == PermissionStatus.denied ||Platform.isAndroid){
      Map<Permission, PermissionStatus> statuses = await [
        Permission.photos,
        // Permission.camera,
        Permission.storage
      ].request();
      if (statuses[Permission.storage] != PermissionStatus.granted && Platform.isAndroid) {
        NSDialogUtil.showConfirmDialog('Permission settings', description: 'The album permissions have been turned off. Do you want to go to Settings to open it?',
            sureAction: () async {
              bool isOpened = await openAppSettings();
            });
      }
      if (statuses[Permission.storage] == PermissionStatus.granted || statuses[Permission.photos] == PermissionStatus.granted){
        onSuccessAction?.call();
      }
    }else if (_permissionStatus == PermissionStatus.permanentlyDenied){
      NSDialogUtil.showConfirmDialog('Permission settings', description: 'The album permissions have been turned off. Do you want to go to Settings to open it?',
          sureAction: () async {
            bool isOpened = await openAppSettings();
          });
    }else if (_permissionStatus == PermissionStatus.granted){
      onSuccessAction?.call();
    }
  }

  static reqCameraPermission ({Function? onSuccessAction}) async {
    PermissionStatus _permissionStatus = await Permission.camera.status;
    if (_permissionStatus == PermissionStatus.denied ||Platform.isAndroid){
      Map<Permission, PermissionStatus> statuses = await [
        Permission.camera,
      ].request();
      if (statuses[Permission.camera] != PermissionStatus.granted && Platform.isAndroid) {
        NSDialogUtil.showConfirmDialog('Permission settings', description: 'The camera permissions have been turned off. Do you want to go to Settings to turn them on?',
            sureAction: () async {
              bool isOpened = await openAppSettings();
            });
      }
      if (statuses[Permission.camera] == PermissionStatus.granted){
        onSuccessAction?.call();
      }
    }else if (_permissionStatus == PermissionStatus.permanentlyDenied){
      NSDialogUtil.showConfirmDialog('Permission settings', description: 'The camera permissions have been turned off. Do you want to go to Settings to turn them on?',
          sureAction: () async {
            bool isOpened = await openAppSettings();
          });
    }else if (_permissionStatus == PermissionStatus.granted){
      onSuccessAction?.call();
    }
  }

  static reqMicrophonePermission ({Function? onSuccessAction}) async {
    PermissionStatus _permissionStatus = await Permission.microphone.status;
    if (_permissionStatus == PermissionStatus.denied ||Platform.isAndroid){
      Map<Permission, PermissionStatus> statuses = await [
        Permission.microphone,
      ].request();
      if (statuses[Permission.microphone] != PermissionStatus.granted && Platform.isAndroid) {
        NSDialogUtil.showConfirmDialog('Permission settings', description: 'The microphone permission is turned off. Do you want to go to Settings to turn it on?',
            sureAction: () async {
              bool isOpened = await openAppSettings();
            });
      }
      if (statuses[Permission.microphone] == PermissionStatus.granted) {
        onSuccessAction?.call();
      }
    }else if (_permissionStatus == PermissionStatus.permanentlyDenied){
      NSDialogUtil.showConfirmDialog('Permission settings', description: 'The microphone permission is turned off. Do you want to go to Settings to turn it on?',
          sureAction: () async {
            bool isOpened = await openAppSettings();
          });
    }else if (_permissionStatus == PermissionStatus.granted){
      onSuccessAction?.call();
    }
  }
}